package com.focus.xyz.core.common.listeners;

import com.focus.xyz.core.utils.ResourceUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chenmeng on 2016/4/5.
 */
public class ErrorDataSplitListener extends TestListenerAdapter {
    private transient static Logger logger = LoggerFactory.getLogger(ErrorDataSplitListener.class);

    @Override
    public void onTestFailure(ITestResult tr) {
        super.onTestFailure(tr);
        logger.info(tr.getName() + " Failure");
        Object[] paras = tr.getParameters();
        String com = (String) paras[7];
        try {
            ResourceUtil.appendFileContent(ConfigPropInfo.TEST_SRCSHOT_DIR + com + "_" +
                    new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())) + ".txt",paras);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("记录投保失败单子异常.");
        }
    }

    @Override
    public void onTestSkipped(ITestResult tr) {
        super.onTestSkipped(tr);
        logger.info(tr.getName() + " Skipped");
        Object[] paras = tr.getParameters();
        String com = (String) paras[7];
        try {
            ResourceUtil.appendFileContent(ConfigPropInfo.TEST_SRCSHOT_DIR + com + "_" +
                    new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())) + ".txt",paras);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("记录投保失败单子异常.");
        }
    }

    @Override
    public void onTestSuccess(ITestResult tr) {
        super.onTestSuccess(tr);
        logger.info(tr.getName() + " Success");
    }

    @Override
    public void onTestStart(ITestResult tr) {
        super.onTestStart(tr);
        logger.info(tr.getName() + " Start");
    }

    @Override
    public void onFinish(ITestContext testContext) {
        super.onFinish(testContext);
    }
}
